/*
 * @Author: wangc
 * @Date: 2020-07-27 14:41:48
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-08-30 15:47:16
 * @Description: 
 */
package com.example.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.example.util.RespCode;
import com.example.util.RespEntity;
import com.example.model.Customer;
import com.example.model.User;
import com.example.service.CustomerService;
import com.example.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
    @Autowired
    private UserService userService;
    @Autowired
    private CustomerService customerService;
    @RequestMapping("/login")
    @ResponseBody
    public RespEntity login(@RequestParam Map<String,Object> params){
        Map<String,Object> map = new HashMap<String,Object>();
        User user = userService.checkLogin(params.get("uid").toString(),params.get("psd").toString());
        if(Objects.isNull(user)){
            return new RespEntity(RespCode.LOGINERROR,"账号密码错误");
        }
        map.put("username", user.getUsername());
        RespEntity resp =new RespEntity(RespCode.SUCCESS,map);
        return resp;
    }

    @RequestMapping("/getAllUser")
    @ResponseBody
    private List<User> getAllUser() {
        List<User> users =  userService.getAllUser();
        return users;

    }

    @RequestMapping("/getDataByNameKey")
    @ResponseBody
    /**
     * 根据name的关键字查找对应是数据
     * @param params
     * @return
     */
    private List<Customer> getDataByNameKey(@RequestParam Map<String,Object> params) {
        List<Customer> results = customerService.getDataByNameKey(params.get("key").toString());
        return results;
    }
}
